package com.m3839.sdk.ym.core

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.os.Process
import com.m3839.sdk.ym.base.YMBaseDialogFragment
import java.util.*
import kotlin.collections.HashMap

/**
 * Activity堆栈管理类
 *
 * @author 陈宝阳
 * @create 2019/4/16
 */
class YMUIStackManager private constructor() {
    /**
     * Activity堆栈
     */
    private val fragmentMap: MutableMap<String, YMBaseDialogFragment> = mutableMapOf()

    /**
     * 添加Fragment到堆栈中
     *
     * @param tag
     * @param fragment
     */
    fun addFragment(tag: String, fragment: YMBaseDialogFragment) {
        fragmentMap[tag] = fragment
    }

    /**
     * 移除指定tag的Fragment
     */
    fun removeFragment(tag: String) {
        if (fragmentMap.contains(tag)) {
            fragmentMap.remove(tag)
        }
    }

    /**
     * 获取指定tag的Fragment
     */
    fun getFragment(tag: String): YMBaseDialogFragment? {
        if (fragmentMap.contains(tag)) {
            return fragmentMap[tag]
        }
        return null
    }

    /**
     * 结束指定的Fragment
     */
    fun finishFragment(tag: String) {
        if (fragmentMap.contains(tag)) {
            val fragment = fragmentMap.remove(tag)
            fragment!!.dismiss()
        }
    }

    /**
     * 清理所有的Fragment
     */
    fun finishAllFragment() {
        for ((_, value) in  fragmentMap) {
            if (!value.isRemoving) {
                value.dismiss()
            }
        }
        fragmentMap.clear()
    }

    /**
     * 移除自己以外的Fragment
     */
    fun removeAboveFragments(tag: String) {
        for ((key, value) in  fragmentMap) {
            if (tag != key && !value.isRemoving) {
                //val fragment = fragmentMap.remove(key)
                //fragmentMap.remove(key)
                //在onDestroy的时候已经做了移除操作
                value.dismiss()
            }
        }
    }

    val aliveFragment: Int
        get() = fragmentMap.size

    /**
     * 退出app
     */
    fun exitApp(activity: Activity) {
        try {
            finishAllFragment()
            killAllProcess(activity)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun killAllProcess(context: Context) {
        val mActivityManager = context.getSystemService(
            Context.ACTIVITY_SERVICE
        ) as ActivityManager
        val mList =
            mActivityManager.runningAppProcesses
        for (runningAppProcessInfo in mList) {
            if (runningAppProcessInfo.pid != Process.myPid()) {
                Process.killProcess(runningAppProcessInfo.pid)
            }
        }

        //干掉主进程
        //android.os.Process.killProcess(android.os.Process.myPid());
    }

    companion object {
        const val TAG = "YMUIStackManager"

        @Volatile
        private var sInstance: YMUIStackManager? = null
        val instance: YMUIStackManager?
            get() {
                if (sInstance == null) {
                    synchronized(YMUIStackManager::class.java) {
                        if (sInstance == null) {
                            sInstance = YMUIStackManager()
                        }
                    }
                }
                return sInstance
            }
    }
}